/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement;

import Utilities.Constants.MachineStateChangeAction;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * change a machine's execution state is sent to the machines management service.
 *
 * @author Angel Sanadinov
 */
public class ChangeMachineExecutionStateRequest extends MachineManagementRequest
{
    private MachineStateChangeAction stateChangeAction; //the requested change action

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see MachineStateChangeAction#INVALID_ACTION
     */
    public ChangeMachineExecutionStateRequest()
    {
        super();
        this.stateChangeAction = MachineStateChangeAction.INVALID_ACTION;
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_CHANGE_STATE);
    }

    /**
     * Constructs a request for changing the state of a virtual machine.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId  the id of the virtual machine
     * @param stateChangeAction the requested state change action
     */
    public ChangeMachineExecutionStateRequest(int requestorId, int serverId, String machineId, MachineStateChangeAction stateChangeAction)
    {
        setRequestorId(requestorId);
        setServerId(serverId);
        setMachineId(machineId);
        this.stateChangeAction = stateChangeAction;
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_CHANGE_STATE);
    }

    /**
     * Retrieves the machine state change action associated with this request.
     *
     * @return the state change action
     */
    public MachineStateChangeAction getStateChangeAction()
    {
        return stateChangeAction;
    }

    /**
     * Sets the machine state change action for the request.
     *
     * @param stateChangeAction the state change action
     */
    public void setStateChangeAction(MachineStateChangeAction stateChangeAction)
    {
        this.stateChangeAction = stateChangeAction;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && isMachineIdValid() && stateChangeAction != null
           && stateChangeAction != MachineStateChangeAction.INVALID_ACTION)
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (machineId,stateChangeAction)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ChangeMachineExecutionStateRequest.class.getSimpleName()
                + ": (" + getMachineId() + "," + stateChangeAction + ")";
    }
}
